package com.vcommon.core.modules
{
import flash.events.Event;
import flash.system.Capabilities;
import flash.system.Security;

import mx.core.UIComponent;

import org.robotlegs.base.ContextEvent;
import org.robotlegs.utilities.modular.core.IModuleContext;

/**
 * author yangwz
 */		
public class FlexAbstractModule extends UIComponent
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	/**
	 * Constructs a new <code>FlexAbstractModule</code> instance.
	 * 
	 */
	public function FlexAbstractModule(autoRemove:Boolean = false)
	{
		if(autoRemove)
		{
			addEventListener(Event.REMOVED_FROM_STAGE, removedFromStageHandler);
		}
		
		if (Capabilities.playerType != "Desktop")
		{
			Security.allowDomain("*");
			Security.allowInsecureDomain("*");
		}
	}
	//==========================================================================
	//  Variables
	//==========================================================================
	/**
	 * Robotlegs Context
	 */
	protected var context:IModuleContext;
	//==========================================================================
	//  Methods
	//==========================================================================
	public function dispose():void
	{
		removeEventListener(Event.REMOVED_FROM_STAGE, removedFromStageHandler);
		var contextEvent:ContextEvent = new ContextEvent(ContextEvent.SHUTDOWN);
		context.eventDispatcher.dispatchEvent(contextEvent);
		context.dispose();
		context = null;
	}
	//==========================================================================
	//  Event Handlers
	//==========================================================================
	/**
	 * 当该模块从舞台上移除时触发的事件处理.
	 * @param event
	 *
	 */
	private function removedFromStageHandler(event:Event):void
	{
		dispose();
	}
}
}